package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysLog;
import com.powernode.model.Result;
import com.powernode.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author RenBoQing
 * @date 2024年07月21日 12:17
 * @Description 系统操作日志控制器
 */
@Api(tags = "系统日志接口管理")
@RequestMapping("sys/log")
@RestController
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;
    /*
     * 多条件分页查询系统日志
     * @author RenBoQing
     * @date 2024/7/21 12:36
     * @param current
     * @param size
     * @param userId
     * @param operator
     * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.SysLog>>
     */
    @ApiOperation("多条件分页查询系统日志")
    @RequestMapping("/page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page<SysLog>> saveSysLog(@RequestParam(defaultValue = "1") Long current,
                                           @RequestParam(defaultValue = "10") Long size,
                                           @RequestParam(required = false) Long userId,
                                           @RequestParam(required = false) String operator) {
        // 创建分页对象
        Page<SysLog> page = new Page<>(current, size);
        page = sysLogService
                 .page(page, new LambdaQueryWrapper<SysLog>()
                // 如果查询条件eq和like都存在，优先使用eq
                .eq(ObjectUtil.isNotNull(userId), SysLog::getUserId, userId)
                .like(operator != null, SysLog::getOperation, operator)
                .orderByDesc(SysLog::getCreateDate)
        );
        // 返回分页查询的数据
        return Result.success(page);
    }
}
